package com.huaxiafinance.management.controller;

import com.huaxiafinance.management.dto.request.GradeDto;
import com.huaxiafinance.management.dto.request.MemberGrowthDto;
import com.huaxiafinance.management.service.MemberGradeService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller("memberGradeController")
@RequestMapping("/memberGrade")
public class MemberGradeController {

  @Autowired
  private MemberGradeService memberGradeService;

  private final static Logger log = Logger.getLogger(MemberGradeController.class);

  /**
   * 请求成长倍数值
   *
   * @return 成长倍数值
   */
  @RequestMapping(value = "/queryMemberGrowthBase", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryMemberGrowthBase() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = memberGradeService.queryMemberGrowthBase();
    } catch (Exception e) {
      log.info("修改成长基础倍数异常原因" + e);
    }
    log.info("修改成长基础倍数返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 修改积分成长倍数
   *
   * @param dto 修改积分成长倍数请求值
   * @return 返回修改成功与否
   */
  @RequestMapping(value = "/updateMemberGrowthBase", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateMemberGrowthBase(@Valid @RequestBody MemberGrowthDto dto,
      BindingResult bindingResult) {
    log.info("修改成长基础倍数请求参数" + dto);
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
      return messageResult.getAsJSON();
    }
    try {
      messageResult = memberGradeService.updateMemberGrowthBase(dto);
    } catch (Exception e) {
      log.info("修改成长基础倍数异常原因" + e);
    }
    log.info("修改成长基础倍数返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 查询会员成长倍数列表
   *
   * @return 会员成长倍数
   */
  @RequestMapping(value = "/queryMemberGrade", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String queryMemberGrade() {
    MessageResult messageResult = new MessageResult();
    try {
      messageResult = memberGradeService.queryMemberGrade();
    } catch (Exception e) {
      log.info("查询会员成长倍数列表异常原因" + e);
    }
    log.info("查询会员成长倍数列表返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

  /**
   * 修改会员成长倍数列表
   *
   * @param dto 请求参数
   * @return 返回修改成功否
   */
  @RequestMapping(value = "/updateMemberGrade", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String updateMemberGrade(@Valid @RequestBody GradeDto dto, BindingResult bindingResult) {
    log.info("修改成长基础倍数请求参数" + dto);
    MessageResult messageResult = new MessageResult();
    if (bindingResult.hasErrors()) {
      messageResult.setFailureResult(bindingResult);
      return messageResult.getAsJSON();
    }
    try {
      messageResult = memberGradeService.updateMemberGrade(dto);
    } catch (Exception e) {
      log.info("修改成长基础倍数异常原因" + e);
    }
    log.info("修改成长基础倍数返回参数" + messageResult.getAsJSON());
    return messageResult.getAsJSON();
  }

}